<template>
	<el-container class="container">
	  <el-header class="header">
		  <div>
			  <img src="../assets/img/logo1.png" />
			  <span>后台管理系统</span>
		  </div>
		  <el-button type="info" @click="logout">退出</el-button>
	  </el-header>
	  <el-container>
	    <el-aside width="200px" class="aside">
			<el-menu
			    default-active="2"
			    class="el-menu-vertical-demo"
			    background-color="blueviolet"
				unique-opened
				router
			    text-color="#fff"
			    active-text-color="#C397D8"
				:default-active="activePath"
				>
				<!-- 可展开的菜单 -->
			    <el-submenu :index="item.id" v-for="item in menuList">
			      <template slot="title">
			        <i :class="iconList[item.id]"></i>
			        <span>{{item.authName}}</span>
			      </template>
				  <!-- 二级菜单 -->
			      <el-menu-item :index="'/'+item1.path"
				      v-for="item1 in item.children"
					  @click="saveActivePath('/'+item1.path)"
					  >
					  <i class="el-icon-menu"></i>
					  <span>{{item1.authName}}</span>
				  </el-menu-item>
			      </el-submenu>
			</el-menu>
		</el-aside>
	    <el-main>
			<router-view></router-view>
		</el-main>
	  </el-container>
	</el-container>
</template>

<script>
	export default {
		data() {
			return {
				menuList: [],
				iconList: {
					'101': 'el-icon-s-goods',
					'102': 'el-icon-s-order',
					'103': 'el-icon-s-cooperation',
					'125': 'el-icon-s-custom',
					'145': 'el-icon-s-data'
				},
				activePath: ''
			}
		},
		methods: {
			logout() {
				window.sessionStorage.clear();
				this.$router.push('/login');
			},
			async getMenuList() {
				let {data: res} = await this.$http.get('/permission/menus');
				console.log(res);
				if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
				this.menuList = res.data;
			},
			saveActivePath(activePath) {
				//保存激活状态到sessionStorage中
				window.sessionStorage.setItem('activePath',activePath)
				this.activePath = activePath;
			}
		},
		created() {
			this.getMenuList();
			//获得被激活的菜单
			this.activePath = window.sessionStorage.getItem('activePath')
		}
	}
</script>

<style lang="scss">
	.container{
		height: 100%;
	}
	.header{
		background-color: #6B6BB8;
		display: flex;
		color: #fff;
		align-items: center;
		justify-content: space-between;
		>div{
			display: flex;
			align-items: center;
			font-size: 17px;
			font-weight: 900;
		}
	}
	.aside{
		background-color: #6B6BB8;
	}
</style>
